package com.geek688.auxiliarytool.util;

import java.math.BigDecimal;

public class ClassUtil {

	public static <T> T readWithClassType(Object obj, Class<T> clazz) {
        if (isNormalJavaType(clazz)) {
            String s = obj + "";
            if (clazz == String.class) {
                return clazz.cast(s);
            } else if (clazz == int.class || clazz == Integer.class) {
                return clazz.cast(Integer.parseInt(s));
            } else if (clazz == long.class || clazz == Long.class) {
                return clazz.cast(Long.parseLong(s));
            } else if (clazz == boolean.class || clazz == Boolean.class) {
                return clazz.cast(Boolean.parseBoolean(s));
            } else if (clazz == float.class || clazz == Float.class) {
                return clazz.cast(Float.parseFloat(s));
            } else if (clazz == double.class || clazz == Double.class) {
                return clazz.cast(Double.parseDouble(s));
            } else if (clazz == char.class || clazz == Character.class) {
                return clazz.cast(s);
            } else if (clazz == short.class || clazz == Short.class) {
                return clazz.cast(Short.parseShort(s));
            } else if (clazz == byte.class || clazz == Byte.class) {
                return clazz.cast(Byte.parseByte(s));
            } else if (clazz == BigDecimal.class) {
                return clazz.cast(Long.parseLong(s));
            } else {
                throw new IllegalArgumentException("Unknown class type[" + clazz + "] to read!");
            }
        } else {
            return clazz.cast(obj);
        }
    }
	public static boolean isNormalJavaType(Class<?> clazz) {
        boolean isNormalJavaType = clazz == Integer.class || clazz == String.class || clazz == Long.class
                                   || clazz == Boolean.class || clazz == Float.class || clazz == Double.class
                                   || clazz == Character.class || clazz == Short.class || clazz == Byte.class
                                   || clazz == BigDecimal.class;
        return clazz.isPrimitive() || isNormalJavaType;
    }
}
